import { TypeSchema } from "./protocol";
import fs from "fs";

class SystemDataType {
    private types: Record<string, TypeSchema>;

    constructor() {
        this.types = {};
        const json_path = "datatypes.json";
        const json_content = fs.readFileSync(json_path, "utf-8");
        const json_data: Record<string, Record<string, any>[]> = JSON.parse(json_content);
        for (const group of Object.values(json_data)) {
            for (const item of group) {
                const name = item.name;
                const def = item.def;
                const short = item.short;
                if (name) {
                    if (def || short) {
                        this.types[name] = TypeSchema.parse(def || short);
                    } else {
                        console.error(`no "def" or "short" for ${name}`);
                    }
                } else {
                    console.error(`no "name" for ${item}`);
                }
            }
        }
    }

    explain(name: string): TypeSchema | null {
        return this.types[name] || null;
    }
}

function test() {
    const system_data_type = new SystemDataType();
}

export default SystemDataType;

test();